package com.zsh.resource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsh.common.result.CommonResult;
import com.zsh.resource.domain.DishRecLog;
import com.zsh.resource.domain.MemberDishLog;
import com.zsh.resource.domain.RecLog;
import com.zsh.resource.mapper.DishRecLogMapper;
import com.zsh.resource.mapper.MemberDishLogMapper;
import com.zsh.resource.service.RecLogService;
import com.zsh.resource.mapper.RecLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author 18179
* @description 针对表【rec_log】的数据库操作Service实现
* @createDate 2023-03-13 12:06:08
*/
@Service
public class RecLogServiceImpl extends ServiceImpl<RecLogMapper, RecLog>
    implements RecLogService{

    private final MemberDishLogMapper memberDishLogMapper;
    private final DishRecLogMapper dishRecLogMapper;
    public RecLogServiceImpl(MemberDishLogMapper memberDishLogMapper, DishRecLogMapper dishRecLogMapper) {
        this.memberDishLogMapper = memberDishLogMapper;
        this.dishRecLogMapper = dishRecLogMapper;
    }
    /**
     * 收藏与否
     * @param type 【0 - 取消收藏】 【1 - 收藏】
     */
    @Transactional
    @Override
    public CommonResult<Object> updateCollect(Integer type, String dishId, String memberId) {
        LambdaQueryWrapper<RecLog> recLogWrapper = new LambdaQueryWrapper<>();
        recLogWrapper.eq(RecLog::getDishId, dishId).eq(RecLog::getMemberId, memberId);
        int count = this.count(recLogWrapper);
        if (count > 0) {
            this.baseMapper.updateCollectType(type == 1, dishId, memberId);
        } else {
            RecLog recLog = new RecLog();
            recLog.setIsCollect(true);
            recLog.setClickNum(0);
            recLog.setCommentNum(0);
            recLog.setDishId(dishId);
            recLog.setIsUp(false);
            recLog.setMemberId(memberId);
            this.save(recLog);
        }
        int var = type == 1 ? 1 : -1;
        dishRecLogMapper.updateCollectTimes(var, dishId);
        return CommonResult.success();
    }

    /**
     * 是否取消点赞 【0 - 取消点赞】 【1 - 点赞】
     */
    @Override
    public CommonResult<Object> updateUp(String userId, String dishId, Integer type) {
        LambdaQueryWrapper<RecLog> recLogWrapper = new LambdaQueryWrapper<>();
        recLogWrapper.eq(RecLog::getDishId, dishId).eq(RecLog::getMemberId, userId);
        int count = this.count(recLogWrapper);
        if (count > 0) {
            this.baseMapper.updateUpType(type == 1, dishId, userId);
        } else {
            RecLog recLog = new RecLog();
            recLog.setIsUp(true);
            recLog.setClickNum(0);
            recLog.setCommentNum(0);
            recLog.setDishId(dishId);
            recLog.setIsUp(false);
            recLog.setMemberId(userId);
            this.save(recLog);
        }
        // 如果是点赞，则全局日志表 + upNum = 1
        int addOrSub = type == 1 ? 1 : -1;
        dishRecLogMapper.addUpTimes(dishId, addOrSub);
        return CommonResult.success();
    }

    @Transactional
    @Override
    public CommonResult<Object> saveClickDishLog(String userId, String dishId) {
        // 记录全局日志
        LambdaQueryWrapper<DishRecLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishRecLog::getDishId, dishId);
        int count = dishRecLogMapper.selectCount(wrapper);
        if (count > 0) {
            dishRecLogMapper.addTimes(dishId);
        } else {
            DishRecLog dishRecLog = new DishRecLog();
            dishRecLog.setDishId(dishId);
            dishRecLog.setClickNum(1);
            dishRecLog.setCommentNum(0);
            dishRecLog.setCollectNum(0);
            dishRecLog.setUpNum(0);
            dishRecLogMapper.insert(dishRecLog);
        }
        // 记录局部日志
        LambdaQueryWrapper<MemberDishLog> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(MemberDishLog::getDishId, dishId);
        wrapper2.eq(MemberDishLog::getMemberId, userId);
        int count2 = memberDishLogMapper.selectCount(wrapper2);
        if (count2 > 0) {
            memberDishLogMapper.addTimes(userId, dishId);
        } else {
            MemberDishLog memberDishLog = new MemberDishLog();
            memberDishLog.setDishId(dishId);
            memberDishLog.setMemberId(userId);
            memberDishLog.setClickNum(1);
            memberDishLog.setCommentNum(0);
            memberDishLog.setIsCollect(false);
            memberDishLog.setIsUp(false);
            memberDishLogMapper.insert(memberDishLog);
        }
        return CommonResult.success();
    }
}




